package com.nh.service;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.nh.dao.BaseDao;
import com.nh.entity.BaseEntity;
import com.nh.utils.CopyUtils;
import com.nh.utils.Page;

@Component("baseService")
public class BaseService implements IBaseService {
	private BaseDao baseDao;

	@Override
	public void attachClean(BaseEntity bean) {
		baseDao.attachClean(bean);
	}

	@Override
	public void attachDirty(BaseEntity bean) {
		baseDao.attachDirty(bean);
	}

	@Override
	public void delete(BaseEntity bean) {
		baseDao.delete(bean);
	}

	@Override
	public void delete(Long id, Class<? extends BaseEntity> clazz) {
		baseDao.delete(id, clazz);
	}

	@Override
	public void delete(String propertyName, Object value, Class<? extends BaseEntity> clazz) {
		baseDao.delete(propertyName, value, clazz);
	}

	@Override
	public <T extends BaseEntity> List<T> findAll(Class<T> clazz) {
		return baseDao.findAll(clazz);
	}

	@Override
	public <T extends BaseEntity> List<T> findAll(Class<T> clazz, int index, int count) {
		Criteria criteria = this.getBaseDao().getSession().createCriteria(clazz);
		criteria.setFirstResult(index);
		criteria.setMaxResults(count);
		return criteria.list();
	}

	@Override
	public <T extends BaseEntity> T findEntity(Class<T> clazz) {
		Criteria criteria = this.getBaseDao().getSession().createCriteria(clazz);
		return (T) criteria.list().get(0);
	}

	@Override
	public <T extends BaseEntity> List<T> findAll(Class<T> clazz, LinkedHashMap<String, String> orderInfos) {
		return baseDao.findAll(clazz, orderInfos);
	}

	@Override
	public <T extends BaseEntity> List<T> findByExample(T t) {
		return baseDao.findByExample(t);
	}

	@Override
	public <T extends BaseEntity> List<T> findByExample(T t, LinkedHashMap<String, String> orderInfos) {
		return baseDao.findByExample(t, orderInfos);
	}

	@Override
	public <T extends BaseEntity> T findById(long id, Class<T> clazz) {
		return baseDao.findById(id, clazz);
	}

	@Override
	public <T extends BaseEntity> List<T> findByProperty(String propertyName, Object value, Class<T> clazz) {
		return baseDao.findByProperty(propertyName, value, clazz);
	}

	@Override
	public <T extends BaseEntity> List<T> findByProperty(String propertyName, Object value, Class<T> clazz, LinkedHashMap<String, String> orderInfos) {
		return baseDao.findByProperty(propertyName, value, clazz, orderInfos);
	}

	@Override
	public boolean isExist(long id, Class<? extends BaseEntity> clazz) {
		return baseDao.isExist(id, clazz);
	}

	@Override
	public <T extends BaseEntity> T merge(T bean) {
		return baseDao.merge(bean);
	}

	@Override
	public void save(BaseEntity bean) {
		baseDao.save(bean);
	}

	@Resource
	@Override
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public BaseDao getBaseDao() {
		return this.baseDao;
	}

	@Override
	public Page findBy(String sql, int pageNumber, int pageSize) {
		return this.baseDao.findBy(sql, pageNumber, pageSize);
	}

	@Override
	public Page findBy(Query query, int pageNumber, int pageSize) {
		return this.baseDao.findBy(query, pageNumber, pageSize);
	}

	@Override
	public Page findBy(Criteria criteria, int pageNumber, int pageSize) {
		return this.baseDao.findBy(criteria, pageNumber, pageSize);
	}

	@Override
	public Page findPageBy(Criteria criteria, int pageNumber, int pageSize) {
		return this.baseDao.findPageBy(criteria, pageNumber, pageSize);
	}

	@Override
	public <T extends BaseEntity> List<T> findAll(Class<T> clazz, String orderByPropertyName, String orderByType) {
		return baseDao.findAll(clazz, orderByPropertyName, orderByType);
	}

	@Override
	public <T extends BaseEntity> List<T> findByExample(T t, String orderByPropertyName, String orderByType) {
		return baseDao.findByExample(t, orderByPropertyName, orderByType);
	}

	@Override
	public <T extends BaseEntity> List<T> findByProperty(String propertyName, Object value, Class<T> clazz, String orderByPropertyName, String orderByType) {
		return baseDao.findByProperty(propertyName, value, clazz, orderByPropertyName, orderByType);
	}

	public void saveOrUpdate(BaseEntity bean, Class<? extends BaseEntity> clazz) {
		if (null != bean.getId()) {
			BaseEntity old = this.baseDao.findById(bean.getId(), clazz);
			CopyUtils.copyNotNullProperties(old, bean);
			bean = old;
		}
		this.baseDao.save(bean);

	}

	@Override
	public List<Object> findBySql(String sql) {
		return baseDao.findBySql(sql);
	}

}
